package com.ruoyi.shebei.service.impl;

import java.util.List;

import com.ruoyi.common.utils.DateUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.shebei.mapper.ShebeiMapper;
import com.ruoyi.shebei.domain.Shebei;
import com.ruoyi.shebei.service.IShebeiService;

/**
 * 设备Service业务层处理
 * 
 * @author admin
 * @date 2024-07-26
 */
@Service
public class ShebeiServiceImpl implements IShebeiService 
{
    @Autowired
    private ShebeiMapper shebeiMapper;
    @Autowired
    private SqlSessionFactory sqlSessionFactory;


    /**
     * 查询设备
     * 
     * @param shebeiId 设备主键
     * @return 设备
     */
    @Override
    public Shebei selectShebeiByShebeiId(Long shebeiId)
    {
        return shebeiMapper.selectShebeiByShebeiId(shebeiId);
    }

    /**
     * 查询设备列表
     * 
     * @param shebei 设备
     * @return 设备
     */
    @Override
    public List<Shebei> selectShebeiList(Shebei shebei)
    {
        return shebeiMapper.selectShebeiList(shebei);
    }

    /**
     * 新增设备
     * 
     * @param shebei 设备
     * @return 结果
     */
    @Override
    public int insertShebei(Shebei shebei)
    {
        shebei.setCreateTime(DateUtils.getNowDate());
        return shebeiMapper.insertShebei(shebei);
    }


    /**
     * 批量新增设备
     *
     * @param shebeis 设备List
     * @return 结果
     */
    @Override
    public int batchInsertShebei(List<Shebei> shebeis)
    {
        SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH, false);
        int count = 0;
        if (!CollectionUtils.isEmpty(shebeis)) {
            try {
                for (int i = 0; i < shebeis.size(); i++) {
                    int row = shebeiMapper.insertShebei(shebeis.get(i));
                    // 防止内存溢出，没100次提交一次,并清除缓存
                    boolean bool = (i >0 && i%3 == 0) || i == shebeis.size() - 1;
                    if (bool){
                        sqlSession.commit();
                        sqlSession.clearCache();
                    }
                    count = i + 1;
                }
            }catch (Exception e){
                e.printStackTrace();
                // 没有提交的数据可以回滚
                sqlSession.rollback();
            }finally {
                sqlSession.close();
                return count;
            }
        }
        return count;
    }


    /**
     * 修改设备
     * 
     * @param shebei 设备
     * @return 结果
     */
    @Override
    public int updateShebei(Shebei shebei)
    {
        return shebeiMapper.updateShebei(shebei);
    }

    /**
     * 批量删除设备
     * 
     * @param shebeiIds 需要删除的设备主键
     * @return 结果
     */
    @Override
    public int deleteShebeiByShebeiIds(Long[] shebeiIds)
    {
        return shebeiMapper.deleteShebeiByShebeiIds(shebeiIds);
    }

    /**
     * 删除设备信息
     * 
     * @param shebeiId 设备主键
     * @return 结果
     */
    @Override
    public int deleteShebeiByShebeiId(Long shebeiId)
    {
        return shebeiMapper.deleteShebeiByShebeiId(shebeiId);
    }

    /**
     * 设备总个数
     *
     * @param shebeiId 设备主键
     * @return 结果
     */
    @Override
    public int selectcount(Long shebeiId) {
    int count = shebeiMapper.selectcount(shebeiId);
        System.out.println(count);
        return count;
    }
}
